package com.common.utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;

	/**
	 *显示对象的一些方法包装 
	 * @author lake
	 * 
	 */	
	public class DisplayObjectUtil
	{
		/**
		 *显示对象灰化 
		 */		
		public static const DISABLE_FILTER :ColorMatrixFilter = 
			new ColorMatrixFilter([0.5,0.5,0.5,0,0,
			0.5,0.5,0.5,0,0,
			0.5,0.5,0.5,0,0,
			0,0,0,1,0,]);
		public function DisplayObjectUtil()
		{
		}
		/**
		 * 清除子对象 
		 * @param container 目标
		 * @param mode flase---只除第一子对象 true -- 所有嵌套子对象,并销毁位图
		 * 
		 */			
		public static function removeAllChildren(container:DisplayObjectContainer,mode :Boolean = false):void
		{
			if(container == null)
			{
				return;
			}
			var disP :DisplayObject;
			while(container.numChildren > 0)
			{
				disP = container.getChildAt(0);
				if(mode && (disP is DisplayObjectContainer))
				{
					removeAllChildren(disP as DisplayObjectContainer, mode);
				}
				if(mode && (disP is Bitmap))
				{
					if((disP as Bitmap).bitmapData)(disP as Bitmap).bitmapData.dispose();
				}
				container.removeChildAt(0);
			}
		}
		/**
		 *深复制显示元件 
		 * @param disp
		 * @return 
		 */		
		public static function deepCopy(disp:DisplayObject):*
		{
			var classStr :String = getQualifiedClassName(disp);
			var classMC :* = getDefinitionByName(classStr);
			var newObj :* = new classMC();
			return newObj;
		}
		/**
		 *获取挖空效果的sprite 
		 * @param mkx
		 * @param mky
		 * @param mkw
		 * @param mkh
		 * @param gw
		 * @param gh
		 * @param color
		 * @param alp
		 * @param rectY 第二个区域
		 * @return 
		 * 
		 */		
		public static function getRectMarkSprite(mkx:Number,
										   mky:Number,
										   mkw:Number,
										   mkh:Number,
										   gw:Number,
										   gh:Number,
										   color:Number = 0x434343,
										   alp:Number = .7,
										   rectY:Rectangle = null):Sprite
		{
			var sp :Sprite = new Sprite();
			sp.graphics.beginFill(color,alp);
			sp.graphics.drawRect(0,0,gw,mky);
			sp.graphics.endFill();
			sp.graphics.beginFill(color,alp);
			sp.graphics.moveTo(0,mky);
			sp.graphics.drawRect(0,mky,mkx,gh - mky);
			sp.graphics.endFill();
			sp.graphics.beginFill(color,alp);
			sp.graphics.moveTo(mkx + mkw,mky);
			sp.graphics.drawRect(mkx + mkw,mky,gw - mkx - mkw,gh - mky);
			sp.graphics.endFill();
			sp.graphics.beginFill(color,alp);
			sp.graphics.moveTo(mkx,mky + mkh);
			if(rectY && rectY.y == (mky + mkh))
			{
				sp.graphics.drawRect(mkx,mky + mkh,rectY.x - mkx,gh - mky - mkh);
				sp.graphics.endFill();
				sp.graphics.beginFill(color,alp);
				sp.graphics.moveTo(rectY.x,rectY.y + rectY.height);
				sp.graphics.drawRect(rectY.x,rectY.y + rectY.height,rectY.width,gh - mky - mkh - rectY.height);
				sp.graphics.endFill();
				sp.graphics.beginFill(color,alp);
				sp.graphics.moveTo(rectY.x + rectY.width,rectY.y);
				sp.graphics.drawRect(rectY.x + rectY.width,rectY.y,gw - mkx - rectY.width,gh - mky - mkh);
				sp.graphics.endFill();
			}
			else
			{
				sp.graphics.drawRect(mkx,mky + mkh,mkw,gh - mky - mkh);
			}
			sp.graphics.endFill();
			return sp;
		}
	}
}